home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 8 / The Arsenal Files Collection #8 (Arsenal Computer) (1996).ISO / prg_gen / euphor14.zip / SORT.E < prev    next >
Text File  |  1995-10-14  |  1KB  |  48 lines

  1.         -------------
  2.         -- Sorting --
  3.         -------------
  4.  
  5. -- Sort a sequence of arbitrary objects into ascending order.
  6.  
  7. -- If you want to change the default ordering of objects in Euphoria
  8. -- write your own global compare() function to override the builtin compare,
  9. -- then include this file after it.
  10.  
  11. global function sort(sequence x)
  12. -- Shell sort will sort any sequence of Euphoria objects. Shell sort is
  13. -- a "stable" sort, i.e. it will not change the order of elements that 
  14. -- are considered equal.
  15.  
  16.     integer gap, j, first, last
  17.     object tempi, tempj
  18.  
  19.     last = length(x)
  20.     gap = floor(last / 3) + 1
  21.     while 1 do
  22.     first = gap + 1
  23.     for i = first to last do
  24.         tempi = x[i]
  25.         j = i - gap
  26.         while 1 do
  27.         tempj = x[j]
  28.         if compare(tempi, tempj) >= 0 then
  29.             j = j + gap
  30.             exit
  31.         end if
  32.         x[j+gap] = tempj
  33.         if j <= gap then
  34.             exit
  35.         end if
  36.         j = j - gap
  37.         end while
  38.         x[j] = tempi
  39.     end for
  40.     if gap = 1 then
  41.         return x
  42.     else
  43.         gap = floor(gap / 3) + 1
  44.     end if
  45.     end while
  46. end function
  47.  
  48.